// 
//   FILE:  dht11_test1.pde
// PURPOSE: DHT11 library test sketch for Arduino
// http://arduino.cc/playground/Main/DHT11Lib
/* Dec 2, 2011: This code is used for Arduino with two DHT11s
   and equipped with Xbee (Pro Series 1) to send data over serial
*/
/*
 The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 */
#include <LiquidCrystal.h>
#define LCD_Print(x,y,t) lcd.setCursor(x,y);lcd.print(t)
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
	return 1.8 * celsius + 32;
}

//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
	return celsius + 273.15;
}

// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm 
double dewPoint(double celsius, double humidity)
{
	double A0= 373.15/(273.15 + celsius);
	double SUM = -7.90298 * (A0-1);
	SUM += 5.02808 * log10(A0);
	SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
	SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
	SUM += log10(1013.246);
	double VP = pow(10, SUM-3) * humidity;
	double T = log(VP/0.61078);   // temp var
	return (241.88 * T) / (17.558-T);
}

// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
	double a = 17.271;
	double b = 237.7;
	double temp = (a * celsius) / (b + celsius) + log(humidity/100);
	double Td = (b * temp) / (a - temp);
	return Td;
}

void printSerial(void);
void printSerial2(void);

#include <dht11.h>

dht11 DHT11,DHT11B;

#define DHT11PIN 6
#define DHT11BPIN 7

void setup()
{
  Serial.begin(9600);
  Serial.println("DHT11 TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT11LIB_VERSION);
  Serial.println();
  pinMode(DHT11BPIN,INPUT);
  pinMode(DHT11PIN,INPUT);
  lcd.begin(16, 2);
  LCD_Print(0,1,"CRMA SenseNet");
}

void loop()
{
//  Serial.print(analogRead(0));
//  Serial.print(", ");
//  Serial.print(analogRead(1));
//  Serial.println("\n");
  int chk = DHT11.read(DHT11PIN);
  delay(1000);
  switch (chk)
  {
    case 0: { Serial.print("DHT11A: "); printSerial(); break; }
    case -1: Serial.println("DHT11A: Checksum error"); break;
    case -2: Serial.println("DHT11A: Time out error"); break;
    default: Serial.println("DHT11A: Unknown error"); break;
  }
  chk = DHT11B.read(DHT11BPIN); // read another DHT11
//  Serial.println("\n");
  delay(1000);
  switch (chk)
  {
    case 0:  { Serial.print("DHT11B: "); printSerial2(); break; }
    case -1: Serial.println("DHT11B: Checksum error"); break;
    case -2: Serial.println("DHT11B: Time out error"); break;
    default: Serial.println("DHT11B: Unknown error"); break;
  }
}

void printSerial(void) {
  LCD_Print(0,0,"TH1:            ");
  LCD_Print(4,0,DHT11.temperature);
  LCD_Print(9,0,DHT11.humidity);
//  Serial.print("Humidity (%): ");
  Serial.print((float)DHT11.humidity, 2);
//  Serial.print("Temperature (oC): ");
  Serial.print(" : ");
  Serial.print((float)DHT11.temperature, 2);

//  Serial.print("Temperature (oF): ");
  Serial.print(" : ");
  Serial.print(Fahrenheit(DHT11.temperature), 2);

//  Serial.print("Temperature (K): ");
  Serial.print(" : ");
  Serial.print(Kelvin(DHT11.temperature), 2);

//  Serial.print("Dew Point (oC): ");
  Serial.print(" : ");
  Serial.print(dewPoint(DHT11.temperature, DHT11.humidity));

//  Serial.print("Dew PointFast (oC): ");
  Serial.print(" : ");
  Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));
}

void printSerial2(void) {
  LCD_Print(0,1,"TH2:            ");
  LCD_Print(4,1,DHT11B.temperature);
  LCD_Print(9,1,DHT11B.humidity);
//  Serial.print("Humidity (%): ");
  Serial.print((float)DHT11B.humidity, 2);

//  Serial.print("Temperature (oC): ");
  Serial.print(" : ");
  Serial.print((float)DHT11B.temperature, 2);

//  Serial.print("Temperature (oF): ");
  Serial.print(" : ");
  Serial.print(Fahrenheit(DHT11B.temperature), 2);

//  Serial.print("Temperature (K): ");
  Serial.print(" : ");
  Serial.print(Kelvin(DHT11B.temperature), 2);

//  Serial.print("Dew Point (oC): ");
  Serial.print(" : ");
  Serial.print(dewPoint(DHT11B.temperature, DHT11B.humidity));

//  Serial.print("Dew PointFast (oC): ");
  Serial.print(" : ");
  Serial.println(dewPointFast(DHT11B.temperature, DHT11B.humidity));
}

//
// END OF FILE
//
